package kr.co.skplanet.commute.controller.api;

import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import kr.co.skplanet.commute.domain.Member;
import kr.co.skplanet.commute.service.MemberService;
import kr.co.skplanet.commute.util.APIUtils;
import kr.co.skplanet.commute.util.StringUtils;

import org.jboss.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hazelcast.impl.Util;

@Controller
public class MemberAPIController {
	@Autowired
	MemberService memberService;

//	@ResponseBody	
//	@RequestMapping( value = { "/member/list.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
//	public List<Member> memberlist() {	
//		
//		List<Member> list = memberService.getMemberList();
//		return list;
//	}
	@ResponseBody	
	@RequestMapping( value = { "/member/get.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public HashMap<String, Object> get(
			@RequestParam(value = "email", required = false) String email,
			@RequestParam(value = "id", required = false) Long id
			) {	
		try {
			Member member = null;
			if(id != null) {
				member = memberService.getMember(id);
			} else if(!email.isEmpty()) {
				member = memberService.getMember(StringUtils.genSHA256(email));
			}
			
			if(member != null)
				return APIUtils.getResultHashMap(true, "ok", member);
			else
				return APIUtils.getResultHashMap(false, "member not found");
		} catch (NullPointerException e) {
			return APIUtils.getResultHashMap(false, "invalid argument");
		}
	}
	@ResponseBody
	@RequestMapping(value = { "/member/auth.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> memberlist(
			@RequestParam(value = "email", required = false) String email,
			@RequestParam(value = "passwd", required = false) String passwd,
			@RequestParam(value = "auth_key", required = false) String authkey
			) {
		HashMap<String, String> result = new HashMap<String, String>();

		try {
			Member member = null;
			if(authkey != null) {
				member = memberService.getMemberByAuthKey(authkey);
			} else if(email != null && passwd != null) {
				member = memberService.getMember(email, passwd);
			} else
				throw new NullPointerException();
			
			if (member == null) {
				return APIUtils.getResultHashMap(false, "INVALID ID OR PASSWORD");
			} else {
				if(authkey == null) {
					member.setEmail(email);
					member.setPassword(passwd);
					member.setSalt(StringUtils.genRandomStringWithSpecialCharacter(20));
					memberService.saveMember(member);
				}
				
				result.put("memberId", Long.toString(member.getId()));
				result.put("APIKey", "");
				result.put("authKey", member.getAuthKey());
				
				return APIUtils.getResultHashMap(true, "ok", result);
			}
		} catch (NullPointerException e) {
			return APIUtils.getResultHashMap(false, "invalid arguments");
		}
	}

	@ResponseBody
	@RequestMapping(value = { "/member/register.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> memberregister(
			@RequestParam(value = "email", required = true) String email,
			@RequestParam(value = "passwd", required = true) String passwd) {
		
		if(!Pattern.matches(".+@.+\\..+", email)) {
			return APIUtils.getResultHashMap(false, "EMAIL ADDRESS TYPE IS INVALID");
		} else if(memberService.getMember(email) != null) {
			return APIUtils.getResultHashMap(false, "EMAIL ADDRESS ALREADY EXISTS");
		} else if(passwd.isEmpty()) {
			return APIUtils.getResultHashMap(false, "PASSWORD REQUIRED");
		}
		
		Member member = new Member();
		member.setEmail(email);
		member.setPassword(passwd);
		member = memberService.saveMember(member);

		HashMap<String, String> result = new HashMap<String, String>();
		result.put("memberId", Long.toString(member.getId()));
		result.put("APIKey", "");
		result.put("authKey", member.getAuthKey());
		
		return APIUtils.getResultHashMap(true, "ok", result);
	}
}
